// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: donggu, jhkim

#include "coin2/base/config/app_config.h"
#include "coin2/base/config/args.h"
#include "coin2/base/log.h"
#include "coin2/strategy/linear_model/strategy.h"

using namespace coin2::exchange::base::symbology;
using namespace coin2::base::config;
using namespace coin2::exchange::feed;
using namespace coin2::app;
using ::coin2::strategy::linear_model::LmStrategy;

int main(int argc, char* argv[]) {
  InitModules();

  DriverConfig app;

  cxxopts::Options opt("lm-agg pilot", "lm-agg Live Pilot");
  opt.add_options()("lm_strat_json", "model config path", cxxopts::value<std::string>());
  opt.add_options()("kline_json", "kline path for feature",
      cxxopts::value<std::string>()->default_value(""));
  opt.add_options()("dryrun", "if dryrun, enable OE && disable OG", cxxopts::value<bool>());
  opt.add_options()(
      "auto_shutdown_sec",
      "auto shutdown after x sec, default is 1000000 secs",
      cxxopts::value<int64_t>()->default_value("1000000"));
  AddDriverOptions(&opt);
  AddIntervalOptions(&opt);
  AddProductOptions(&opt);

  auto res = opt.parse(argc, argv);
  ParseDriverOptions(res, &app);
  ParseIntervalOptions(
      res,
      app.mutable_feed()->mutable_common()->mutable_archive()->mutable_interval());
  ParseProductOptions(res, app.mutable_feed());

  if (res.count("dryrun") != 0U) {
    app.mutable_strategy()->mutable_common()->mutable_common()->set_dryrun(true);
  }

  app.mutable_log()->set_log_file(fmt::format("/tmp/coin2/log/{}", app.strategy().strategy_name()));

  ::coin2::base::InitLogging(argv[0], app.log());

  LmStrategy lm_strategy(
      res["lm_strat_json"].as<std::string>(),
      res["kline_json"].as<std::string>(),
      GetCurrentTimestamp());
  Driver driver(app, &lm_strategy);
  driver.RegisterCleanupHandler();
  driver.RegisterCleanupTimeout(res["auto_shutdown_sec"].as<int64_t>());
  driver.Run();

  return 0;
}
